package com.campus.backend.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.campus.backend.entity.Delivery;
import com.campus.backend.service.DeliveryService;
import com.campus.common.bean.common.PageQueryParam;
import com.campus.common.bean.common.Pagination;
import com.campus.common.bean.common.Resp;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.campus.backend.bean.delivery.DeliveryUpdateBean;
import cn.hutool.core.bean.BeanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;

/**
 * <p>
 * 配送员表 前端控制器
 * </p>
 *
 * @author ZhaoYuJie
 * @since 2025-05-21
 */
@Api(tags = "配送员信息管理接口")
@RestController
@RequestMapping("/delivery")
public class DeliveryController {

    @Autowired
    private DeliveryService deliveryService;

    /**
     * 分页查询配送员信息列表
     * @param queryParam 查询参数
     * @return 分页结果
     */
    @ApiOperation("分页查询配送员信息列表")
    @GetMapping("/list")
    public Resp<Pagination<Delivery>> getDeliveryList(PageQueryParam queryParam, String name, String phone, Integer workStatus) {
        PageInfo<Delivery> pageInfo = PageHelper.startPage(queryParam.getPageNo(), queryParam.getPageSize())
                .doSelectPageInfo(() -> {
                    LambdaQueryWrapper<Delivery> queryWrapper = new LambdaQueryWrapper<>();
                    queryWrapper.like(StringUtils.isNotBlank(name), Delivery::getName, name)
                            .like(StringUtils.isNotBlank(phone), Delivery::getPhone, phone)
                            .eq(workStatus != null, Delivery::getWorkStatus, workStatus)
                            .orderByDesc(Delivery::getCreateTime);
                    deliveryService.list(queryWrapper);
                });
        return Resp.successPageable(pageInfo);
    }

    /**
     * 更新配送员信息
     * @param deliveryUpdateBean 配送员更新信息
     * @return 操作结果
     */
    @ApiOperation("更新配送员信息")
    @PutMapping("/update")
    public Resp<Void> updateDelivery(@Valid @RequestBody DeliveryUpdateBean deliveryUpdateBean) {
        // 查询配送员记录
        Delivery existDelivery = deliveryService.getById(deliveryUpdateBean.getId());
        if (existDelivery == null) {
            return Resp.error("配送员记录不存在");
        }
        
        // 使用BeanUtil复制属性
        Delivery updateDelivery = new Delivery();
        BeanUtil.copyProperties(deliveryUpdateBean, updateDelivery);
        updateDelivery.setUpdateTime(LocalDateTime.now());
        
        deliveryService.updateById(updateDelivery);
        return Resp.success();
    }

    /**
     * 获取配送员详情
     * @param id 配送员ID
     * @return 配送员详情
     */
    @ApiOperation("获取配送员详情")
    @GetMapping("/detail/{id}")
    public Resp<Delivery> getDeliveryDetail(@PathVariable Long id) {
        Delivery delivery = deliveryService.getById(id);
        if (delivery == null) {
            return Resp.error("配送员记录不存在");
        }
        return Resp.success(delivery);
    }
}
